package BL;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Anmelden
 */
@WebServlet("/Anmelden")
public class Anmelden extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Anmelden() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println( request.getParameter("user") );
		
		
		
		/* Parameter abfragen */
		/*String usertext = request.getParameter("user");
		if (usertext != null)
			usertext = usertext.trim();
		String cmd = request.getParameter("cmd");

		/* Liste aller angemeldeten Benutzer */
		/*LinkedList<String> allUsers;
		Object o;
		o = this.getServletContext().getAttribute("all");
		allUsers = (LinkedList<String>) o;
		if (allUsers == null)
			allUsers = new LinkedList<String>();

		HttpSession sess = request.getSession();

		String errStr = "";
		String user = "";
		if (cmd != null && cmd.equalsIgnoreCase("ANMELDEN")) {
			if (usertext.length() == 0)
				errStr = "Leerstring nicht erlaubt.";
			else if (allUsers.contains(usertext))
				errStr = "Benutzer '" + usertext + "' existiert bereits.";
			// In 2 Browsern einer Session melden sich unterschiedliche Benutzer
			// an ...
			// else if ((user=(String)session.getAttribute("user"))!=null)
			// errStr = "Da hat '" + user + "' dazwischen gefunkt.";
			else {
				allUsers.add(usertext);
				sess.setAttribute("user", usertext);
			}
		}
		if (cmd != null && cmd.equalsIgnoreCase("ABMELDEN")) {
			user = (String) sess.getAttribute("user");
			if (allUsers != null && user != null)
				allUsers.remove(user);
			sess.removeAttribute("user");
		}
		if (cmd != null && cmd.equalsIgnoreCase("AUFFRISCHEN")) {
			; // nichts zu tun :-)
		}
		getServletContext().setAttribute("all", allUsers);
		user = (String) sess.getAttribute("user");
		PrintWriter out = response.getWriter();
		out.println("<html><title>Anmeldung</title><body>");
		out.println(errStr.length() != 0 ? "<font color=\"#FF0000\">" + errStr
				+ "</font>" : "");
		out.println("<form method=\"get\" action=\"Anmelden\">");
		out.println(user == null ? "Nicht angemeldet" : "Angemeldet: " + user);
		out.println("<br /> " + "Alle Benutzer: " + allUsers);

		if (user == null) { // noch anzumelden
			out.println("<br /> " + "<input name=\"user\" type=\"text\">");
			out.println("<input name=\"cmd\" type=\"submit\" value=\"Anmelden\">");
		} else
			// ist angemeldet
			out.println("<input name=\"cmd\" type=\"submit\" value=\"Abmelden\">");

		out.println("<br /><input name=\"cmd\" type=\"submit\" value=\"Auffrischen\">");
		out.print("</form></body></html>");*/
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
